package smoker.client;

import java.io.*;
import java.util.*;

import smoker.tools.*;

public class ClientCenter implements IClientCenter{
	private final String FILENAME = "src/smoker/client/LogLoginInfo.properties";
	private User currentUser;
	private IClient client;
	private LoginGUI loginGui;
	private Vector<Department> vAllDptName;
	private Vector<User> vOnLineUser;
	private ClientGUI clientGui;
	private String serverIP;
	private int port;
	private Properties p;
	
	public ClientCenter() {
		p = new Properties();
		vAllDptName = new Vector<Department>();
		vOnLineUser = new Vector<User>();
		loginGui = new LoginGUI(this);
		loginGui.setVisible(true);
		client = new Client(this);
	}
	
	public void login(String loginName, String password) {
			User user = new User();
			user.setLoginID(loginName);
			user.setPassword(password);
			client.login(user);
	}
	
	public void onLineInfromAboutAllUser(User user) {
		clientGui.addUserNode(user);
	}

	public void onLoginResult(RadiateData data) {
		currentUser = data.getCurrentUser();
		if(currentUser != null) {
			this.vAllDptName = data.getDepartmentList();
			this.vOnLineUser = data.getOnLineUserList();
			clientGui = new ClientGUI(this, currentUser);
			loginGui.onLoginResult(true);
			clientGui.setVisible(true);
		}else {
			loginGui.onLoginResult(false);
		}
	}
	
	public void sendMessage(int recevicerID, String msg, String dateTime){
		ChatInfo chatInfo = new ChatInfo();
		chatInfo.setSenderID(currentUser.getUserID());
		chatInfo.setReceiverID(recevicerID);
		chatInfo.setDateTime(dateTime);
		chatInfo.setMessage(msg);
		Message message = new Message(MsgType.TYPE_CHAT, chatInfo);
		client.sendMessage(message);
	}
	
	public void reciveMessage(ChatInfo chatInfo){
		clientGui.reciveMessage(chatInfo);
	}
	
	public static void main(String[] args) {
		new ClientCenter();
	}

	public Vector<Department> getDptName() {
		return vAllDptName;
	}

	public Vector<User> getOnLineUser() {
		return vOnLineUser;
	}

	public void offLineInfromAboutAllUser(User offLineUser) {
		Message msg = new Message(MsgType.TYPE_OFFLINE, offLineUser);
		client.sendMessage(msg);
	}

	public void delOffLineUserNode(User delOffUser) {
		clientGui.removeUserNode(delOffUser);
	}

	public void offLineAndCloseSocket() {
		client.offLineAndCloseSocket();
		
	}

	public User getCurrentUser() {
		return currentUser;
	}

	public boolean connServer() {
		return client.connServer();
		
	}
	
	public void getHistoryNumbers(){
		
	}
	
	public void saveHistoryNumbers(){
		
	}
	
	public void setServerInfo(String serverIP, int port) {
		this.serverIP = serverIP;
		this.port = port;
	}

	public String[] getJICC() {
		String jicc = readFile(FILENAME, "JICC");
		return jicc.split(",");
	}
	
	public String getIP() {
		String jicc = readFile(FILENAME, "IP");
		return jicc;
	}
	
	private String readFile(String filename, String type){
		try {
			InputStream is = new FileInputStream(filename);
			p.load(is);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p.getProperty(type);
	}
	
	public void saveJICC(Vector<String> jicc) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < jicc.size(); i++) {
			if(jicc.get(i).toString().trim().length() == 0)continue;
			sb.append(jicc.get(i) + ",");
		}
		writeFile(FILENAME, "JICC", sb.toString());
	}
	
	public void saveIP(String ip) {
		writeFile(FILENAME, "IP", ip);
	}
	
	public void clearInfo() {
		writeFile(FILENAME, "JICC", "");
	}
	                       
	private void writeFile(String filename, String key, String value) {
		try {
			OutputStream os = new FileOutputStream(filename);
			p.setProperty(key, value);
			p.store(os, "");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
	public String getServerIP() {
		return serverIP;
	}

	public int getPort() {
		return port;
	}

	public void theUserIsExist() {
		loginGui.theUserIsExist();
	}

	public void closeClient() {
		clientGui.closeMe();
	}

	public void updateUser(User user) {
		client.updateUser(user);
	}

	public void onUpdateResult(int result) {
			clientGui.updateResult(result);
	}

	public void coerceOffLine() {
		clientGui.coerceOffLine();
		
	}
}
